package com.googlecode.yoohoo.utils;

public class FilterCreator {
	private static final String LEFT_BRACKET = "(";
	private static final String RIGHT_BRACKET = ")";
	private static final String EQUAL_OPERATOR = "=";
	private static final char NOT_OPERATOR = '!';
	private static final char AND_OPERATOR = '&';
	private static final char OR_OPERATOR = '|';
	
	public static String equal(String key, String value) {
		StringBuffer buf = new StringBuffer();
		
		buf.append(LEFT_BRACKET).
			append(key).
			append(EQUAL_OPERATOR).
			append(value).
			append(RIGHT_BRACKET);
		
		return buf.toString();
	}
	
	public static String not(String filter) {
		StringBuffer buf = new StringBuffer();
		
		buf.append(LEFT_BRACKET).
			append(NOT_OPERATOR).
			append(filter).
			append(RIGHT_BRACKET);
		
		return buf.toString();
	}
	
	public static String and(String filter1, String filter2) {
		return logic(AND_OPERATOR, filter1, filter2);
	}
	
	public static String or(String filter1, String filter2) {
		return logic(OR_OPERATOR, filter1, filter2);
	}
	
	private static String logic(char logicOperator,
			String filter1, String filter2) {
		return logic(Character.toString(logicOperator),
				filter1, filter2);
	}
	
	private static String logic(String logicOperator,
			String filter1, String filter2) {
		StringBuffer buf = new StringBuffer();
		
		buf.append(LEFT_BRACKET).
			append(logicOperator).
			append(filter1).
			append(filter2).
			append(RIGHT_BRACKET);
		
		return buf.toString();
	}
}

